package com.rockwell.sniffyhunter.model; 

import com.rockwell.sniffyhunter.utils.IStateMachine;
import com.rockwell.sniffyhunter.utils.RobotState;

public class StatefulRobot extends StateObject {

	private SnifferObject sniffObj;
	private MoveToNextZoneObject moveToNextZoneObj;
	private Picker pickObj;
	private Unloader unloadObj;
	
	public StatefulRobot(Robot robot) {
		super(robot);
	}
	
	@Override
	public void Initialize() {
		machineState = RobotState.START;
		sniffObj = new SnifferObject(robot);
		moveToNextZoneObj = new MoveToNextZoneObject(robot);
		pickObj = new Picker(robot);
		unloadObj = new Unloader(robot);
	}

	@Override
	public boolean IsDone() {
		return machineState == RobotState.DONE;
	}

	public IStateMachine Sniff() {
		ReturnResult returnVal = sniffObj.PerformAction();
		if (returnVal == ReturnResult.Completed) {
			if (sniffObj.IsTreasureFound())
				machineState = RobotState.PICK_TREASURE;
			else if (sniffObj.IsBasketFull()) //TODO we may need a time out to trigger unload, instead of waiting for the basket to be full.
				machineState = RobotState.UNLOAD;
			else
				machineState = RobotState.MOVE_TO_NEXT_ZONE;
		}
		return machineState;
	}

	public IStateMachine MoveToNextZone() {
		ReturnResult returnVal = moveToNextZoneObj.PerformAction();
		if (returnVal == ReturnResult.Completed) {
			if (moveToNextZoneObj.IsDestinationReached())
				machineState = RobotState.SNIFF;
		}
			
		return machineState;
	}
	
	public IStateMachine PickTreasure() {
		ReturnResult returnVal = pickObj.PerformAction();
		if (returnVal == ReturnResult.Completed) {
			machineState = RobotState.SNIFF;	
		}
		
		return machineState;
	}
	
	public IStateMachine UnloadTreasure() {
		ReturnResult returnVal = unloadObj.PerformAction();		
		if (returnVal == ReturnResult.Completed) {
			machineState = RobotState.SNIFF;
		}
		
		return machineState;
	}
}